﻿$(function () {
    var shortly = new Date();
    $("#timer").contador({
        untilTimer: shortly,
        compact: true,
        onExpiry: TimerOFF,
        layout: '{m10}{m1} : {s10}{s1}'
    });
    IniciarTimer();
});

function IniciarTimer() {
    $("#timer").hide();
    shortly = new Date();
    var segundos = 0;

    $.ajax({
        type: "POST",
        url: "/carrinho.aspx/TempoDeReserva",
        contentType: "application/json; charset=utf-8",
        data: "{ }",
        dataType: "json",
        success: function (result) {
            var retorno = eval('(' + result.d + ')');
            if (retorno.Ret == 1) {
                segundos = retorno.Msg;
                shortly.setSeconds(shortly.getSeconds() + segundos);
                $('#timer').contador('change', { untilTimer: shortly });
                $("#timer").show("fast");
                $("#imgTimer").show();
            }
            else {
                $("#imgTimer").hide();
                $("#timer").text("");
            }
        }
    });
}

function TimerOFF() {
    $.ajax({
        type: "POST",
        url: "/carrinho.aspx/ExpirarReservasCarrinho",
        contentType: "application/json; charset=utf-8",
        data: "{ }",
        dataType: "json",
        async: false,
        success: function (result) {
            var retorno = eval('(' + result.d + ')');

            $("#imgTimer").hide();
            $("#timer").text("");
            ValorTotal = 0;
            FecharCarrinho();
            MontarNotificacoes();

            ExibirErroBalaoPosicional("Seus ingressos foram expirados.", "Carrinho");

            var sPath = window.location.pathname;
            var sPage = sPath.substring(sPath.lastIndexOf('/') + 1).toLowerCase();

            switch (sPage) {
                case "identificacao.aspx":
                case "formadepagamento.aspx":
                case "confirmacaotef.aspx":
                case "confirmacao.aspx":
                case "confirmacaoitau.aspx":
                case "confirmacaohsbc.aspx":
                case "itaupagamento.aspx":
                case "checkout.aspx":
                case "taxaentrega.aspx":
                case "pagamento.aspx":
                    document.location = "Carrinho.aspx";
                    break;
            }
        }
    });
}